import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { finalize, tap } from 'rxjs/operators';

@Injectable()
export class SecurityRequestInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    let token = localStorage.getItem('token');
    if (null != token) {
      req = req.clone({ setHeaders: { Authorization: token } });
    } else {
        if (req.url === '/system/api/searchHumanName' || req.url === '/system/api/login' || req.url === '/system/api/template' || req.url === '/system/api/unitinfo/loadaddress' ) {
            req = req.clone({ setHeaders: { Authorization: 'eyJhbGciOiJIUzUxMiJ9.eyJISSI6LTEsIlVJIjoxLCJITiI6IumZtuaYjum-mSIsIlJJIjoiIiwiVU4iOiLlub_opb_lo67ml4_oh6rmsrvljLoiLCJVQyI6Ii8xLyJ9.DAWCZpyQSAngIv4NBIxxz19mrLGvHdZI8WdNim5wlI0yZoa0WkHCrb-tyDY4v1MpbpEl4TWEOndnZmxxdma0YQ'}})
        }
    }
    return next
      .handle(req)
      .pipe(
        tap(
        ),
        finalize(() => {
        })
      );
  }
}
